package net.dodoman.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class ReadAFile {

	public static void main(String[] args) {
		writeFileFromList(readFileByLine());
	}

	/**
	 * 按行读取
	 * 
	 * @return
	 */
	private static List<String> readFileByLine() {
		File file = new File("E:/Media/abc.txt");
		List<String> linklist = new LinkedList<String>();
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line = br.readLine();
			if (line != null)
				linklist.add(line);
			do {
				line = br.readLine();
				if (line != null)
					linklist.add(line);
			} while (line != null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		for (String str : linklist)
			System.out.println(str);
		return linklist;
	}

	private static boolean writeFileFromList(List<String> list) {
		File file = new File("E:/Media/abc1.txt");
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			// file.createNewFile();
			int size = list.size();
			for (int i = 0; i < size; i++) {
				bw.write(list.get(i));
				bw.newLine();
			}
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}
}
